﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;

public partial class frmCertificateList : BaseWinFrom
{
    public frmCertificateList()
    {
        InitializeComponent();
    }

    void DataBind()
    {
        this.dgList.AutoGenerateColumns = false;
        Model.Search.CertificateList ms = new Model.Search.CertificateList();
        if (!string.IsNullOrEmpty(txtId.Text))
        {
            ms.Id = txtId.Text;
        }
        if (!string.IsNullOrEmpty(txtName.Text))
        {
            ms.Name = txtName.Text;
        }
        if (!string.IsNullOrEmpty(txtCreateDate.Text))
        {
            DateTime dt;
            if (DateTime.TryParse(txtCreateDate.Text, out dt))
            {
                ms.MinCreate = FastJSON.PHP_DateTimeTransform.PHP_DT2LN(dt).ToString();
                ms.MaxCreate = FastJSON.PHP_DateTimeTransform.PHP_DT2LN(dt.AddDays(1)).ToString();
            }
        }
        if (cbStatus.CheckState != CheckState.Indeterminate)
        {
            ms.Status = cbStatus.Checked ? "1" : "0";
        }
        this.dgList.DataSource = new BindingList<Model.Certificate>(new RST.Certificate().List(ms));
    }

    private void btOK_Click(object sender, EventArgs e)
    {
        DataBind();
    }

    private void btAdd_Click(object sender, EventArgs e)
    {
        new frmCertificate(null).Show();
    }

    private void dgList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
    }

    void Handle_Delete(object sender)
    {
        DataGridViewCellCollection dgCellCollection = sender as DataGridViewCellCollection;
        if (dgCellCollection == null)
            return;
        RST.Certificate rsp = new RST.Certificate();
        if (rsp.Delete(new Model.Certificate() { Id = string.Format("{0}", dgCellCollection[dgList.Columns["dgColumn_Id"].Index].Value) }) == false)
        {
            MessageBox.Show(string.Format("删除{0}出错", dgCellCollection[dgList.Columns["dgColumn_Name"].Index].Value));
        }
    }

    private void dgList_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex < 0)
            return;
        if (e.ColumnIndex == dgList.Columns["dgColumn_Delete"].Index)
        {
            if (MessageBox.Show("确定删除", "确定", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(Handle_Delete), dgList.Rows[e.RowIndex].Cells);
                dgList.Rows.RemoveAt(e.RowIndex);
            }
        }
        else if (e.ColumnIndex == dgList.Columns["dgColumn_Edit"].Index)
        {
            BindingList<Model.Certificate> li = dgList.DataSource as BindingList<Model.Certificate>;
            Model.Certificate o = li[e.RowIndex];
            new frmCertificate(o).Show();
        }
    }

}